package com.dataimport.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataimport.entity.sourceentity.StudentBaseInfo;
import com.dataimport.entity.targetentity.Department;
import com.dataimport.entity.targetentity.SchoolCalendar;
import com.dataimport.entity.targetentity.Student;
import com.dataimport.service.StudentBaseInfoService;
import com.dataimport.mapper.StudentBaseInfoMapper;
import com.dataimport.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 学生基础信息服务impl
 *
 * @author 邹代鑫
 * @date 2022/09/28
 */
@Service
public class StudentBaseInfoServiceImpl extends ServiceImpl<StudentBaseInfoMapper, StudentBaseInfo> implements StudentBaseInfoService{

    @Autowired
    private StudentBaseInfoMapper studentBaseInfoMapper;

    @Autowired
    private StudentService studentService;

    public List<Student> selectData(Long currentPage){
        //开启数据库2
        DynamicDataSourceContextHolder.push("mysql2");
        //查询全部学生信息
        Page<StudentBaseInfo> page = new Page<>(currentPage,1000);
        Page<StudentBaseInfo> studentBaseInfoPage = studentBaseInfoMapper.selectPage(page, null);
        List<StudentBaseInfo> studentBaseInfos = studentBaseInfoPage.getRecords();
        //过滤数据
        List<Student> studentList = studentBaseInfos.stream().parallel()
                .filter(new Predicate<StudentBaseInfo>() {
                    @Override
                    public boolean test(StudentBaseInfo studentBaseInfo) {
                        //过滤数据
                        return true;
                    }
                })
                .map(new Function<StudentBaseInfo, Student>() {
                    @Override
                    public Student apply(StudentBaseInfo studentBaseInfo) {
                        Student student = new Student();
                        student.setStudentCode(studentBaseInfo.getXh());
                        student.setStudentName(studentBaseInfo.getXm());
                        student.setStudentGrade(studentBaseInfo.getNj());
                        student.setAdmissionDate(DateUtil.parse(studentBaseInfo.getRxrq()));
                        student.setStudentStatus(Integer.valueOf(studentBaseInfo.getXjztm()));
                        student.setDepartmentCode(studentBaseInfo.getYxsh());
                        student.setMajorCode(studentBaseInfo.getZybm());
                        student.setClassesCode(studentBaseInfo.getBjbm());
                        return student;
                    }
                }).collect(Collectors.toList());

        studentList.forEach(System.out::println);
        DynamicDataSourceContextHolder.poll();
        return studentList;
    }

    /**
     * 处理学生
     */
    @Override
    public void dealWithStudent() {
        long currentPage = 0L;
        while (true) {
            currentPage++;
            List<Student> students = selectData(currentPage);
            if (ObjectUtils.isEmpty(students)) {
                break;
            }
            insert(students);
            System.out.println("插入成功");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void insert(List< Student > studentList){
        //开启数据库1
        DynamicDataSourceContextHolder.push("mysql1");
        //插入数据库1
        studentService.saveBatch(studentList,studentList.size());
        DynamicDataSourceContextHolder.poll();
    }
}




